package scuts.core.extensions;

import scuts.core.types.Option;
import scuts.core.types.Either;

class Eithers 
{

  public static function left<A,B>(e:Either<A,B>):A 
  {
    return switch (e) {
      case Left(l): l;
      case Right(r): throw "Error, either has no left value";
    }
  }
  
  public static function right<A,B>(e:Either<A,B>):B 
  {
    return switch (e) {
      case Left(l): throw "Error, either has no right value";
      case Right(r): r;
    }
  }
  
  public static function leftOption<A,B>(e:Either<A,B>):Option<A> 
  {
    return switch (e) {
      case Left(l): Some(l);
      case Right(r): None;
    }
  }
  
  public static function rightOption<A,B>(e:Either<A,B>):Option<B>
  {
    return switch (e) {
      case Left(l): None;
      case Right(r): Some(r);
    }
  }
  
  public static function isLeft<A,B>(e:Either<A,B>):Bool
  {
    return switch (e) {
      case Left(l): true;
      case Right(r): false;
    }
  }
  
  public static function isRight<A,B>(e:Either<A,B>):Bool
  {
    return !isLeft(e);
  }
  
}